<template>
  <div>
    <div class="filter-container" v-if="!listLoading">
      <el-row :gutter="20">
        <el-col :span="2">
          <el-button class="filter-item" style="margin-left: 10px;" @click="handleRefresh" type="primary">点我刷新</el-button>
        </el-col>
        <el-col :span="2" v-for="(item, index) in headButton" :key="index">
          <el-button class="filter-item"
                     :type="item.buttonType || 'primary'"
                     :size="item.buttonSize || 'small'"
                     @click="item.buttonClick"
          >{{item.buttonText}}</el-button>
        </el-col>
        <el-col :span="6">
          <el-input
            placeholder="请输入关键字"
            icon="search"
            v-model="query.keyword"
            :on-icon-click="loadTable">
          </el-input>
        </el-col>
        <el-col v-for="(item, index) in searchExtend" :key="index" :span="item.span || 4">
          <el-select  filterable v-model="item.valueModel" @change="loadTable" :placeholder="item.placeholder || '请选择'">
            <el-option value="" label="全部"></el-option>
            <el-option
              v-for="i in item.option"
              :key="i.value"
              :label="i.label"
              :value="i.value"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="listLoading" :data="tableData" border style="width: 100%">
      <el-table-column v-for="(item, index) in rawOption"
                         :key="index"
                         :formatter="item.formatter"
                         :label="item.label"
                         :width="item.width"
                         :prop="item.propName">
          >
        </el-table-column>
      <el-table-column label="操作" v-if="operation">
        <template scope="scope">
          <el-button v-for="(item, index) in operation"
                     :key="index"
                     :type="item.buttonType || 'primary'"
                     :size="item.buttonSize || 'small'"
                     @click="item.buttonClick(scope.row)"
          >{{item.buttonText}}</el-button>
        </template>
      </el-table-column>

    </el-table>
    <div class="pagination-container" v-if="!listLoading">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="query.pagination.total"
        :page-sizes="[5, 10, 15, 20, 30]"
        :page-size="query.pagination.per_page"
        :current-page.sync="query.pagination.current_page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>

  </div>
</template>

<script>
  import _ from 'lodash'
  import fetch from 'utils/fetch'

  const paramsFilter = (params) => {
    let array = []
    _.forIn(params, (value, key) => {
      if(value) array.push(key)
    })
   return _.pick(params, array)
  }

  export default {
    name: 'table_demo',
    data() {
      return {
//        列表数据
        tableData: null,
//        页码
        total: null,
//        顶部loading条
        listLoading: true,
//        搜索条件
        listQuery: {
          page: 1,
          limit: 5,
          importance: undefined,
          title: undefined,
          type: undefined
        },
        importanceOptions: [1, 2, 3],
        pvData: [],
        tableKey: 0,
        updateId: null,
        query: {
          pagination: {
            total: 0,
            page: 1,
            per_page: 10
          },
          keyword: ''
        }
      }
    },
    props: {
      searchExtend: {
        type: Array,
      },
      requestUrl: {
        type: String,
        required: true
      },
      rawOption: {
        type: Array,
        required: true
      },
      operation: {
        type: Array
      },
      headButton: {
        type: Array
      }
    },
    mounted () {
      this.loadTable()
    },
    methods: {
      async loadTable() {
        this.listLoading = true
        let params = {
          page: this.query.pagination.current_page,
          per_page: this.query.pagination.per_page,
          keyword: this.query.keyword
        }
       if (this.searchExtend) {
         this.searchExtend.forEach((item) => {
           params[item.searchWord] = item.valueModel
         })
       }
        const res = await fetch({
          url: this.requestUrl,
          method: 'get',
          params: paramsFilter(params)
        })
        this.listLoading = false
        this.tableData = res.data
        console.log('data', this.tableData)
        this.query.pagination = res.meta.pagination
      },
      handleSizeChange(val){
        this.query.pagination.per_page = val
        this.loadTable()
      },
      handleCurrentChange(val){
        this.query.pagination.current_page = val
        this.loadTable()
      },
      handleTurnCard (row) {
        this.dialogStatus = 'turnCard';
        this.dialogFormVisible = true;
      },
      handleUpdate(row) {
        this.updateCardFormVisible = true
        this.updateId = row.id
      },
      updateCardReloadList () {
        this.updateCardFormVisible = false
        this.loadTable()
      },
      handleRefresh() {
        this.loadTable()
      }
    }
  }
</script>
